<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Vue中的动画封装</title>
    <script src="../lib/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<!--    添加进入和移除的动态效果，即过渡效果-->
<div id="root">
    <fade :show="show">
        <div>hello world</div>
    </fade>
    <fade :show="show">
        <h1>hello world</h1>
    </fade>
    <button @click="handleClick">切换</button>
</div>
<script type="text/javascript">
    Vue.component('fade', {
        props: ['show'],
        template:
                `
                  <transition @before-enter="handleBeforeEnter" @enter="handleEnter">
                  <slot v-if="show"></slot>
                  </transition>`,
        methods: {
            handleBeforeEnter: function (el) {
                el.style.color = 'red'
            },
            handleEnter: function (el, done) {
                setTimeout(() => {
                    el.style.color = 'yellow'
                    done()
                }, 2000)
            }
        }
    })
    var vm = new Vue({
        el: "#root",
        data: {
            show: false
        },
        methods: {
            handleClick: function () {
                this.show = !this.show
            }
        },
    });
</script>
<style type="text/css">
    /*显示时：第一帧存在，第二帧移除，默认是.v-enter，与上面 name="fade" 保持一致*/
    .fade-enter {
        opacity: 0;
    }

    /*显示时：第二帧出现，整个生命周期都存在，监听 transition 的变化*/
    .fade-enter-active {
        transition: opacity 3s;
    }

    /*隐藏时：第二帧出现*/
    .fade-leave-to {
        opacity: 0;
    }

    /*隐藏时：整个生命周期都存在，动画结束后才被取消，监听 transition 的变化*/
    .fade-leave-active {
        transition: opacity 3s;
    }
</style>
</body>
</html>
